package com.espirit.builder.biz;

import java.util.Map;

import com.espirit.eap.manager.ModelBuilderSupport;
import com.espirit.eap.pagelayout.DetailLayout;
import com.espirit.eap.pagelayout.Element;
import com.espirit.eap.pagelayout.FlatSection;
import com.espirit.eap.pagelayout.GridSection;
import com.espirit.eap.pagelayout.LinkedButton;
import com.espirit.eap.pagelayout.Model;
import com.espirit.eap.pagelayout.ModelField;
import com.espirit.finance.core.AcctItem;
import com.espirit.finance.core.AcctItemScope;

//@Component
public abstract class AcctItemBuilderSupport extends ModelBuilderSupport {
	protected static final String NAME = "name";
	protected static final String ACTIVE = "active";
	protected static final String FIXEDAMOUNT = "fixedAmount";
	protected static final String REMARK = "remark";
	
	protected static final String ACCTITEMREF = "acctItemRef";
	protected static final String STYLE = "style";
	protected static final String PARENT = "parent";
	protected static final String ACCTITEMSCOPES = "acctItemScopes";
	protected static final String FACTOR = "acctItemFactor";
	protected static final String CONDITION = "triggerCondition";
	protected static final String CONDITIONAMOUNT = "conditionAmount";
	
	@Override
	public boolean init() {
		return super.setClass(AcctItem.class, "Acct Item Standard", "标准");
	}

	@Override
	public void buildFields() {
		super.buildFields();
		addModelField(NAME, "Name", "名称");
		addModelField(ACTIVE, "Status", "状态");
		addModelField(FIXEDAMOUNT, "Fixed Amount", "固定金额");
		addModelField(ACCTITEMREF, "Reference", "参考属性");
		addModelField(STYLE, "Strategy", "策略");
		addModelField(REMARK, "Remark", "备注");
		addModelField(ACCTITEMSCOPES, "Scope Define", "区间定义");
		addModelField(FACTOR, "Factor", "参数因子");
		addModelField(CONDITION, "Condition", "条件");
		addModelField(CONDITIONAMOUNT, "Condition Amount", "条件金额");
		addParentField();
	}
	
	protected void addParentField(){
		addModelField(PARENT, "parent", "科目");
	}
	
	protected abstract String getParentClassName();
	protected abstract String getAcctItemRefClassName();
	protected abstract String getAcctItemFactorClassName();
	
	@Override
	protected void addSearchFields() {
	}
	
	@Override
	protected void addListFields() {
		LinkedButton lb = createActiveLinkedButton();
		listElements.add(lb);
		addListField(convertPropertyToField(PARENT, NAME, ACCTITEMREF, FACTOR, STYLE,  FIXEDAMOUNT, REMARK));
	}
	
	@Override
	protected void gatherExproperties() {
		exProperties.put(REQUIRED, new String[]{NAME,  ACTIVE});
	}

	@Override
	protected Element convertFieldToElement(ModelField mf, boolean isListLayout) {
		Element element = super.convertFieldToElement(mf, isListLayout);
		String name = element.getName();
//		if (!isListLayout){
			if (name.equals(PARENT + ".id")){
				Map<String, String> exProperties = element.safeExProperties();
				exProperties.put("model", getParentClassName());
				element.setExProperty(exProperties);
				element.setTagName("modelselect");
			}	
			
			if (name.equals(ACCTITEMREF+ ".id")){
				Map<String, String> exProperties = element.safeExProperties();
				exProperties.put("model", getAcctItemRefClassName());
				element.setExProperty(exProperties);
				element.setTagName("modelselect");
			}
			
			if (name.equals(FACTOR+ ".id")){
				Map<String, String> exProperties = element.safeExProperties();
				exProperties.put("model", getAcctItemFactorClassName());
				element.setExProperty(exProperties);
				element.setTagName("modelselect");
			}
			
			if (CONDITION.equals(name) || CONDITIONAMOUNT.equals(name)){
				if (!isListLayout){
					element.setTagName("textarea");
				}
			}
//		}
		return element;
	}
	
	@Override
	protected void afterBuilderLayout() {
		super.afterBuilderLayout();
		
		DetailLayout dl = model.getDefaultDetailLayout();
		FlatSection scopes = dl.createFlatSection();
		scopes.setNamePrefix("model");
		Model acctItemScopes = getModel(AcctItemScope.class);
		GridSection gs = elementService.createSectionElement(scopes, getModelField(ACCTITEMSCOPES), acctItemScopes.getDefaultListLayout().getMainGridSection());
		gs.setTagName("datagrid");
		gs.setReadOnly(false);
		updateObject(model);
	}

}
